/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.mail.EmailException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.weissenborn.colabtex.client.service.LaTexService;
import com.weissenborn.colabtex.server.helpers.MailHelper;
import com.weissenborn.colabtex.server.tex.TexUtil;

/**
 * The Class LaTexServiceImpl handles async requests from the client refering to latex.
 */
public class LaTexServiceImpl extends RemoteServiceServlet implements LaTexService{
	
	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.service.LaTexService#createOrUpdatePdf(java.lang.String)
	 */
	public String createOrUpdatePdf(String padId) {	
		TexUtil.writePadToPdf(padId);
		return "Pad was compiled!";
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.service.LaTexService#getCompileLog(java.lang.String)
	 */
	public String getCompileLog(String padId) {
		String path = TexUtil.getDirectory(padId)+"/"+padId.substring(padId.lastIndexOf("$")+1)+".log";
		String output = "No log file found! Maybe you didn't compile or compiling is not completed yet.";
		try {
			File file = new File(path);
			if(!file.exists())
				return output;
			FileInputStream in = new FileInputStream(file);
			int c;	
			output = "";
			while ((c = in.read()) != -1) {
			    output+=(char)c;
			}		
			in.close();
	    } catch (IOException e) {
			e.printStackTrace();
		}
		
		return output;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.service.LaTexService#sendPdfFile(java.lang.String, java.lang.String)
	 */
	public Boolean sendPdfFile(String padId, String address) {
		String path = TexUtil.getDirectory(padId)+"/"+padId.substring(padId.lastIndexOf("$")+1)+".pdf";
		
		try {
			MailHelper mailHelper = new MailHelper("com/weissenborn/colabtex/mail.ini", "Find attached pdf file.");
			if(!mailHelper.addAttachment(path))
				return false;
			mailHelper.sendMail(address);
		} catch (EmailException e) {
			return false;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.service.LaTexService#sendTexFile(java.lang.String, java.lang.String)
	 */
	public Boolean sendTexFile(String padId, String address) {
		String path = TexUtil.getDirectory(padId)+"/"+padId.substring(padId.lastIndexOf("$")+1)+".tex";
		
		try {
			MailHelper mailHelper = new MailHelper("com/weissenborn/colabtex/mail.ini", "Find attached pdf file.");
			if(!mailHelper.addAttachment(path))
				return false;
			mailHelper.sendMail(address);
		} catch (EmailException e) {
			return false;
		}
		return true;
	}

	public List<String> getResources(String padId) {
		List<String> result = new ArrayList<String>();
		String path = TexUtil.getDirectory(padId);
		File f = new File(path);
		if(!f.exists())
			return result;
		for(String s:f.list())
			if(!(s.endsWith(".aux")||s.endsWith(".pdf")||s.endsWith(".out")||s.endsWith(".log")||s.endsWith(".tex")||s.endsWith(".toc")))
				result.add(s);
		return result;
	}

	public Boolean deleteResource(String name, String padId) {		
		String path = TexUtil.getDirectory(padId)+"/"+name;
		File f = new File(path);		
		if(!f.exists())
			return true;
		return f.delete();
	}

}
